<template>
  <div class="base-content">
    <div class="basic-info">
      <el-form ref="filterForm" :model="filterForm" inline>
        <el-form-item label="样品编号" prop="sampleNum">
          <el-input
            v-model="filterForm.sampleNum"
            placeholder="样品编号"
            clearable
          />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button
            v-loading="searchLoading"
            type="primary"
            @click="getList('filter')"
            >查询</el-button
          >
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="base-content-operate">
        <el-button
          v-rights="'Generate'"
          icon="el-icon-document-copy"
          type="primary"
          @click="generateReport()"
          >生成报告</el-button
        >
        <el-button
          v-rights="'Withdrawal'"
          icon="el-icon-document-copy"
          type="primary"
          @click="withdrawal()"
          >报告撤回</el-button
        >
        <!-- <el-button
          v-rights="'Export'"
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData()"
          >导出</el-button
        > -->
      </div>
      <el-table
        ref="table"
        v-loading="tableLoad"
        :data="tableData"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="sampleNum" label="样品编号"> </el-table-column>
        <el-table-column prop="orderNum" label="委托单号"> </el-table-column>
        <el-table-column prop="sampleName" label="样品名称"> </el-table-column>
        <el-table-column prop="detectionItemName" label="检测项目" />
        <el-table-column prop="count" label="单份容量" />
        <el-table-column prop="elnRecordCount" label="引用数量" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <div class="base-table-operation">
              <el-link
                v-if="row.elnRecordCount > 1"
                type="primary"
                @click="updateSort(row)"
                >调整顺序</el-link
              >
            </div></template
          ></el-table-column
        >
      </el-table>
      <!-- 分页 -->
      <BasePagination
        :total="total"
        :current-page="pagination.pageIndex"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>
    <GenerateReportDialog
      v-if="dialogFormVisible"
      :dialog-form-visible.sync="dialogFormVisible"
      :row-data.sync="rowData"
    />
    <ElnRecordListDialog
      v-if="innerRecordDialogFormVisible"
      :record-dialog-form-visible.sync="innerRecordDialogFormVisible"
      :sample-info-id.sync="sampleInfoId"
      :entrust-task-id.sync="entrustTaskId"
      :eln-record-ids.sync="elnRecordIds"
      :row-data.sync="rowData"
    />
    <ReportWithdrawalDialog
      v-if="withdrawalDialogFormVisible"
      :withdrawal-dialog-form-visible.sync="withdrawalDialogFormVisible"
      :report-ids="reportIds"
      :sample-registration-ids="sampleRegistrationIds"
      :type="1"
    />
  </div>
</template>
<script>
import GenerateReportDialog from "./GenerateReportDialog.vue";
import { getList } from "../../../api/Report/ReportSampleRegistration.js";
import ElnRecordListDialog from "../../../components/ReportTemplate/ElnRecordListDialog.vue";
import ReportWithdrawalDialog from "../../../components/ReportTemplate/ReportWithdrawalDialog.vue";
export default {
  name: "GenerateReportList",
  components: {
    GenerateReportDialog,
    ElnRecordListDialog,
    ReportWithdrawalDialog,
  },
  data() {
    return {
      searchLoading: false,
      tableLoad: false,
      dialogFormVisible: false,
      innerRecordDialogFormVisible: false,
      withdrawalDialogFormVisible: false,
      sampleRegistrationIds: [],
      reportIds: [],
      filterForm: {
        sampleBusinessStatusCode: [50, 51],
        sampleNum: "",
      },
      tableData: [],
      tableDatas: [],
      pagination: {
        pageIndex: 1,
        pageSize: 10,
      },
      total: 0,
      rowData: [],
      sampleInfoId: "",
      entrustTaskId: "",
      elnRecordIds: [],
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    // 导出
    exportData() {},
    // 生成报告
    generateReport() {
      if (this.tableDatas === null || this.tableDatas.length === 0) {
        this.$message({
          message: "请选择数据",
          type: "warning",
        });
        return false;
      }

      const map = new Map();
      var entrustCount = this.tableDatas.filter(
        (key) => !map.has(key.orderNum) && map.set(key.orderNum, 1)
      );
      if (entrustCount.length !== 1) {
        this.$message.error("请选择相同订单");
        return false;
      }
      // if (this.tableDatas.length > 1) {
      //   this.$message({
      //     message: "只能选择一条数据",
      //     type: "warning",
      //   });
      //   return false;
      // }

      this.rowData = this.tableDatas;
      this.dialogFormVisible = true;
    },
    getList(flag) {
      if (flag) {
        this.pagination.pageIndex = 1;
      }
      this.loading = true;
      getList({ ...this.filterForm, ...this.pagination })
        .then((res) => {
          this.tableData = res.data.dataList ? res.data.dataList : [];
          this.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
    withdrawal() {
      if (this.tableDatas === null || this.tableDatas.length === 0) {
        this.$message({
          message: "请选择数据",
          type: "warning",
        });
        return false;
      }
      this.sampleRegistrationIds = [];

      this.tableDatas.forEach((element) => {
        this.sampleRegistrationIds.push(element.id);
      });
      this.withdrawalDialogFormVisible = true;
    },
    // 清空筛选条件
    cancelForm() {
      this.$refs.filterForm.resetFields();
      this.getList("filter");
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    changePage(page) {
      this.pagination.pageIndex = page;
      this.getList();
    },
    updateSort(rowData) {
      this.sampleInfoId = rowData.sysBusinessSampleInfoId;
      // this.entrustTaskId = rowData.entrustTaskId;
      // this.elnRecordIds = rowData.elnRecordIds;
      this.innerRecordDialogFormVisible = true;
    },
  },
};
</script>
